package com.bus.boot.api.member;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bus.boot.api.ApiResponse;
import com.bus.boot.api.ApiValidate;
import com.bus.boot.api.Constants;
import com.bus.boot.api.ResponseCode;
import com.bus.boot.entity.assist.BusGuestbook;
import com.bus.boot.entity.assist.BusGuestbookCtg;
import com.bus.boot.manager.assist.BusGuestbookCtgMng;
import com.bus.boot.manager.assist.BusGuestbookMng;
import com.bus.common.web.RequestUtils;
import com.bus.common.web.ResponseUtils;
import com.bus.core.entity.BusUser;
import com.bus.core.web.WebErrors;
import com.bus.core.web.util.BusUtils;

//@Controller
public class BusGuestbookApiAct {
	/**
	 * 留言发布API
	 * 
	 * @param siteId
	 *            站点ID 非必选 默认当前站
	 * @param ctgId
	 *            分类ID 必选
	 * @param title
	 *            标题 必选
	 * @param content
	 *            内容 必选
	 * @param email
	 *            邮箱 非必选
	 * @param phone
	 *            电话 非必选
	 * @param qq
	 *            qq号 非必选
	 */
	@RequestMapping(value = "/guestbook/save")
	public void guestbookSave(Integer siteId, Integer ctgId, String title,
			String content, String email, String phone, String qq,
			HttpServletRequest request, HttpServletResponse response)
			throws JSONException {
		String body = "\"\"";
		String message = Constants.API_MESSAGE_PARAM_REQUIRED;
		String code = ResponseCode.API_CODE_PARAM_REQUIRED;
		WebErrors errors = WebErrors.create(request);
		BusUser user = BusUtils.getUser(request);
		// 验证公共非空参数
		errors = ApiValidate.validateRequiredParams(request, errors, ctgId,
				title, content);
		if (!errors.hasErrors()) {
			BusGuestbookCtg ctg = busGuestbookCtgMng.findById(ctgId);
			if (ctg != null) {
				String ip = RequestUtils.getIpAddr(request);
				BusGuestbook guestbook = busGuestbookMng.save(user, siteId,
						ctgId, ip, title, content, email, phone, qq);
				body = "{\"id\":" + "\"" + guestbook.getId() + "\"}";
				message = Constants.API_MESSAGE_SUCCESS;
				code = ResponseCode.API_CODE_CALL_SUCCESS;
			} else {
				message = Constants.API_MESSAGE_OBJECT_NOT_FOUND;
				code = ResponseCode.API_CODE_NOT_FOUND;
			}
		}
		ApiResponse apiResponse = new ApiResponse(request, body, message, code);
		ResponseUtils.renderApiJson(response, request, apiResponse);
	}

	/**
	 * 我的留言API
	 * 
	 * @param siteId
	 *            站点ID 非必选
	 * @param ctgId
	 *            分类ID 非必选
	 * @param appId
	 *            appId 必选
	 * @param sessionKey
	 *            会话标识 必选
	 * @param first
	 *            开始 非必选 默认0
	 * @param count
	 *            数量 非必选 默认10
	 */
	@RequestMapping(value = "/guestbook/mylist")
	public void myGuestbookList(Integer siteId, Integer ctgId, Integer first,
			Integer count, HttpServletRequest request,
			HttpServletResponse response) throws JSONException {
		if (first == null) {
			first = 0;
		}
		if (count == null) {
			count = 10;
		}
		BusUser user = BusUtils.getUser(request);
		List<BusGuestbook> list = null;
		if (user != null) {
			list = busGuestbookMng.getList(siteId, ctgId, user.getId(), null,
					null, true, true, first, count);
		}
		JSONArray jsonArray = new JSONArray();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				jsonArray.put(i, list.get(i).convertToJson());
			}
		}
		String body = jsonArray.toString();
		String message = Constants.API_MESSAGE_SUCCESS;
		String code = ResponseCode.API_CODE_CALL_SUCCESS;
		ApiResponse apiResponse = new ApiResponse(request, body, message, code);
		ResponseUtils.renderApiJson(response, request, apiResponse);
	}

//	@Autowired
	protected BusGuestbookMng busGuestbookMng;
//	@Autowired
	private BusGuestbookCtgMng busGuestbookCtgMng;
}
